package com.hefeng.example.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hefeng.example.domain.GasUserOrder;
import com.hefeng.example.domain.WorkOrderDispositionResult;
import com.hefeng.example.mapper.CustomerManualWorkOrderMapper;
import com.hefeng.example.mapper.GasUserOrderMapper;
import com.hefeng.example.mapper.WorkOrderDispositionResultMapper;
import com.hefeng.example.mapper.WorkOrderDistributionMapper;
import com.hefeng.example.service.SichuangResultService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@DS("slave")
@Service
@AllArgsConstructor
public class SichuangResultServiceImpl extends ServiceImpl<GasUserOrderMapper, GasUserOrder> implements SichuangResultService {


    @Autowired
    private WorkOrderDispositionResultMapper workOrderDispositionResultMapper;

    @Autowired
    private WorkOrderDistributionMapper workOrderDistributionMapper;
    @Autowired
    private CustomerManualWorkOrderMapper customerManualWorkOrderMapper;

    @Autowired
    private GasUserOrderMapper gasUserOrderMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSICHUANG(WorkOrderDispositionResult workOrderDispositionResult) {

//        boolean isTimeMake=true;
//        int status=5;
//        LambdaQueryWrapper<WorkOrderDispositionResult> queryWrapper=new LambdaQueryWrapper<WorkOrderDispositionResult>();
//        LambdaQueryWrapper<DeviceAutomaticAlarmWorkOrder>orderQueryWrapper=new LambdaQueryWrapper<DeviceAutomaticAlarmWorkOrder>();
//        LambdaQueryWrapper<WorkOrderDistribution>distQueryWrapper=new LambdaQueryWrapper<WorkOrderDistribution>();
//        判断工单完成是否超时
//        distQueryWrapper.eq(WorkOrderDistribution::getWorkId,workOrderDispositionResult.getWorkId());
//        WorkOrderDistribution dis=workOrderDistributionMapper.select(workOrderDispositionResult.getWorkId());
//        isTimeMake= DateUtils.getDatePoorMake(dis.getExpectedCompletionTime(),new Date());
//        if (isTimeMake){
//            status=4;
//        }

        //用户手动工单处理
        String Id = workOrderDispositionResult.getWorkId();
        GasUserOrder gasUserOrder = new GasUserOrder();
        //输入的workId要与用户工单orderId匹配
        gasUserOrder.setOrderId(Id);
        //已完成
        gasUserOrder.setOrderState(3);
        //更新时间
        gasUserOrderMapper.updateSCuser(gasUserOrder);
        //新增工单处理表
        workOrderDispositionResult.setStatus(String.valueOf(1));
        workOrderDispositionResult.setCreatedUser(workOrderDispositionResult.getUserId());
        workOrderDispositionResult.setCreatedTime(new Date());
        workOrderDispositionResult.setId(Id);
        workOrderDispositionResult.setDispositionTime(new Date());
        workOrderDispositionResultMapper.WXinsertWorkOrderDispositionResult(workOrderDispositionResult);
    }

    @Override
    public void updateSCuser(WorkOrderDispositionResult workOrderDispositionResult) {
        workOrderDispositionResult.setCreatedUser(workOrderDispositionResult.getUserId());
        workOrderDispositionResult.setCreatedTime(new Date());
        workOrderDispositionResult.setDispositionStatus(String.valueOf(2));
        workOrderDispositionResultMapper.insert(workOrderDispositionResult);
    }

    /**
     * 四川处理中工单
     *
     * @param dispatcher
     * @return
     */
    @Override
    public List<Map> disposeingAlarmWork(String dispatcher) {
        return gasUserOrderMapper.disposeingAlarmWork(dispatcher);
    }

    /**
     * 四川逾期工单
     *
     * @param dispatcher
     * @return
     */
    @Override
    public List<Map> LateWork(String dispatcher) {
        return gasUserOrderMapper.LateWork(dispatcher);
    }

    /**
     * 四川完成工单
     *
     * @param dispatcher
     * @return
     */
    @Override
    public List<Map> finishWork(String dispatcher) {
        return gasUserOrderMapper.finishWork(dispatcher);

    }

    @Override
    public List<Map> WorkOrderDetails(String id) {
        List<Map> list = new ArrayList<>();
        list = gasUserOrderMapper.WorkOrderDetails(id);
        return list;
    }

    @Override
    public List<Map> finishWorkXQ(String id) {
        return gasUserOrderMapper.finishWorkXQ(id);
    }
}
